﻿Imports Microsoft.Win32
Imports System.Reflection
Imports System.Security.Principal
Imports System.Security.AccessControl
Imports System.Security.Permissions.RegistryPermission

Module RegistryModifier
    Friend _USERDOMAIN As String

    Private Sub SET_UserDomain()
        _USERDOMAIN = Environment.UserDomainName & "\" & Environment.UserName
    End Sub

    'Memastikan bahwa user ada pada akun administrator
    'Friend Function IsRunAsAdmin() As Boolean
    '    Dim principal As New WindowsPrincipal(WindowsIdentity.GetCurrent)
    '    Return principal.IsInRole(WindowsBuiltInRole.Administrator)
    'End Function

    Friend Sub GET_AccessToRegistry(ByVal FileConfigurationPath As String)
        Call SET_UserDomain()

        Dim _OWNER As IdentityReference = New NTAccount(Environment.UserDomainName, Environment.UserName)
        Dim _CONFIG_INFO As IO.FileInfo = New IO.FileInfo(FileConfigurationPath)
        Dim _CONFIG_ACL As New FileSecurity

        _CONFIG_ACL.SetOwner(_OWNER)
        _CONFIG_ACL.AddAccessRule(New FileSystemAccessRule(_OWNER, FileSystemRights.Modify, AccessControlType.Allow, PropagationFlags.None, AccessControlType.Allow))
        '_CONFIG_ACL.SetAccessRuleProtection(True, False)
        Try
            '_CONFIG_INFO.SetAccessControl(_CONFIG_ACL)
        Catch ErrHandle As TargetException
            MessageBox.Show("File konfirgurasi tidak ditemukan." & vbCrLf & vbCrLf & "Keterangan:" & vbCrLf & ErrHandle.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ErrHandle As UnauthorizedAccessException
            MessageBox.Show("Akses ditolak." & vbCrLf & vbCrLf & "Keterangan:" & vbCrLf & ErrHandle.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ErrHandle As Exception
            MessageBox.Show("File konfirgurasi tidak ditemukan." & vbCrLf & vbCrLf & "Keterangan:" & vbCrLf & ErrHandle.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Friend Sub PUSH_RegistryKey(ByVal UserDomain As String, ByVal ApplicationKey As String)
        Try
            Dim _REG_SEC As New RegistrySecurity
            _REG_SEC.AddAccessRule(New RegistryAccessRule(UserDomain, RegistryRights.FullControl, AccessControlType.Allow, PropagationFlags.NoPropagateInherit, AccessControlType.Allow))

            Dim _REG_KEY As RegistryKey
            _REG_KEY = Registry.CurrentUser.CreateSubKey(ApplicationKey, RegistryKeyPermissionCheck.Default, _REG_SEC)

            _REG_KEY = Registry.CurrentUser.OpenSubKey(ApplicationKey, True)
            _REG_KEY.SetValue("ConfigurationFile", _FILE_CONF)
            _REG_KEY.Flush()
            If _REG_KEY IsNot Nothing Then
                _REG_KEY.Close()
            End If
            _REG_KEY = Nothing
        Catch ErrKeyNotFound As KeyNotFoundException
            MessageBox.Show("Kunci registry tidak ditemukan" & vbCrLf & "Silahkan hubungi Administrator anda", "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ErrKeyPrevilege As PrivilegeNotHeldException
            MessageBox.Show("Anda tidak mempunyai wewenang untuk merubah registry" & vbCrLf & "Silahkan hubungi Administrator anda", "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ErrKeyReadOnly As ReadOnlyException
            MessageBox.Show("Registry hanya dapat dibaca saja (Read Only)" & vbCrLf & "Silahkan hubungi Administrator anda", "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ErrKeyNullRef As NullReferenceException
            MessageBox.Show("Nilai registry tidak terdefinisikan" & vbCrLf & "Silahkan hubungi Administrator anda", "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Friend Sub LoadTest()
        Call PUSH_ConnectingToServer(FILECONFIG.ServerAddress, FILECONFIG.Port, FILECONFIG.Database, FILECONFIG.Username, FILECONFIG.Password)
        'Call PUSH_Dispose_Active_Connection()
    End Sub

    Friend Sub FirstLoad_GetData()
        Call SET_ConfigurationFile()
        Call GET_AccessToRegistry(_FILE_CONF)
        Call PUSH_RegistryKey(_USERDOMAIN, "SOFTWARE\ArdhaGp\All Apps\")
        'Call GET_ConfigurationFile(0)
        Call GET_ConfigurationFile_Value()
        Call GetValues()
    End Sub
End Module
